package com.onpositive.mediawiki.crawler;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

import com.onpositive.data.serializer.writers.ObjectCollectionWriter;
import com.onpositive.data.units.Unit;
import com.onpositive.data.units.Units;
import com.onpositive.knowledge.model.values.IdentPropertyTransformer;
import com.onpositive.mediawiki.dataminer.MediaWikiUniverse;

import storage.v1.LowLevelStorage;
import storage.v1.ParserV2;
import storage.v1.ProperyDataExtractor;

import com.onpositive.mediawiki.propertyparser.CompositeValueParser;
import com.onpositive.mediawiki.propertyparser.ParsedCategory;
import com.onpositive.mediawiki.serializing.UniverseObjectCollection;
import com.onpositive.mediawiki.serializing.WriterFactory;

import com.onpositive.semantic.model.api.realm.IRealm;
import com.onpositive.wikistorage.PictureExtractor;
import com.onpositive.wikistorage.PropertyAnalyzer;
import com.onpositive.wikistorage.XMLInterface;
import com.onpositive.wikitemplate.WikiTemplateManager;

import com.onpositive.picture.utils.PictureGrabber ;
/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication {

	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
	 */
	@SuppressWarnings("unused")
	public Object start(IApplicationContext context) throws Exception {
		String pth = "http://upload.wikimedia.org/wikipedia/commons/c/cd/Su-27_low_pass.jpg" ;
		String pth1 = "http://www.rusarmy.com/avia/images/su-27.jpg" ;
		PictureGrabber pg = new PictureGrabber(){
			protected int getVerticalSize( byte[] arr ){
				
				return 600 ;
			}
		};
		byte[] pic = pg.grabWikiPictur(pth, 300,200) ;
		
		String str = "tempate1 }} spac  er 1 {{good template {{#tag:ref|template2}} }} spacer 2 {{ template 3";
		int l = str.length() ;
		String str_1 = str.substring( l,l) ;
		str = ParserV2.killTemplates(str,null) ;
		
		String str1 = "[[Biplane]] general [[11111111|2123123123]] purpose aircraft";
		str1 = str1.replaceAll("(\\[\\[)([^\\[\\]\\|]*)([\\|]){0,1}([^\\]\\]]*)(\\]\\])", "$2");
		
		String str2 = "[[aBiplane|asdasdasd]] general purpose aircraft";
		str2 = str2.replaceAll("(\\[\\[)([^\\[\\]\\|]*)([\\|]){0,1}([^\\]\\]]*)(\\]\\])", "$2");
		
		String s0 = "<!-- com1 -->" ;
		String s1 = "part1 <!-- com1 -->" ;
		String s2 = "part1 <!-- com1 --> part2" ;
		String s3 = "part1 <!-- com1 --> part2 <!-- com2 -->" ;
		String s4 = "part1 <!-- com1 --> part2 <!-- com2 --> part3" ;
		String s5 = "com1 --> part2 <!-- com2 --> part3" ;
		String s6 = "part1 <!-- com1 --> part2 <!-- com2 " ;
		
		String _s0 = ParserV2.killComments(s0) ;
		String _s1 = ParserV2.killComments(s1) ;
		String _s2 = ParserV2.killComments(s2) ;
		String _s3 = ParserV2.killComments(s3) ;
		String _s4 = ParserV2.killComments(s4) ;
		String _s5 = ParserV2.killComments(s5) ;
		String _s6 = ParserV2.killComments(s6) ;
		

		String rootPath = "C:\\wikistorage\\" ;
		String universePath = "C:/workspaces/runtime-com.onpositive.dataminer.rcp.application/.metadata/.plugins/com.onpositive.mediawiki.dataminer.wikipedia//storage" ;
		String bigCategoryName = "Fighter aircraft" ;
		String smallCategoryName = "Polish bomber aircraft 1930–1939" ;
		
		Unit u = Units.getUnitByShortName( "kn", "FORCE" ) ;
		
		WikiTemplateManager wtm = new WikiTemplateManager() ;
		wtm.getTemplate( "Aircraft specifications" ) ;
		wtm.getTemplate( "Aircraft specs" ) ;
		wtm.getTemplate( "Aerospecs" ) ;
		wtm.getTemplate( "Infobox weapon" ) ;
		
		wtm.getTemplateDocs( "Aircraft specifications", "\\" ) ;
		wtm.getTemplateDocs( "Aircraft specifications", "/" ) ;
		wtm.getTemplateDocs( "Aircraft specifications", "" ) ;
		wtm.getTemplateDocs( "Aircraft specs", "" ) ;
		wtm.getTemplateDocs( "Aircraft specs", "examples" ) ;
		wtm.getTemplateDocs( "Aircraft specs", "\\examples" ) ;
		wtm.getTemplateDocs( "Aircraft specs", "/examples" ) ;
		

		
		
/*extraction from network*/
//		
//		LowLevelStorage LLStorage = new LowLevelStorage( universePath );
//		LLStorage.extractCategory( bigCategoryName ) ;
//		ParsedCategory cat = LLStorage.getCategory() ;

/*extracting raw data*/
//
//		RawDataExtractor extractor = new RawDataExtractor( rootPath ) ;
//		ParsedCategory category = XMLInterface.loadParsedCategory( rootPath + RawDataExtractor.RAW_DATA_FILE_NAME ) ;
//		if (category==null){
//			category=new ParsedCategory();
//		}
//		CompositeValueParser.grabParserInfo( category.getChracteristicsMap().values() ) ;
//		extractor.extractRawData( categoryName, category ) ;
//		CompositeValueParser.commitParserInfo( category.getChracteristicsMap(), 0 ) ;
//		XMLInterface.saveParsedCategory( rootPath + RawDataExtractor.RAW_DATA_FILE_NAME , category, 3 ) ;
//		CompositeValueParser.flushStats() ;


/*analyzing properties*/
//		LowLevelStorage LLStorage = new LowLevelStorage();
//		ParsedCategory category = XMLInterface.loadParsedCategory( rootPath + RawDataExtractor.RAW_DATA_FILE_NAME ) ;
//		LLStorage.setCategory( category ) ;
//		ProperyDataExtractor propertyExtractor = new ProperyDataExtractor() ;
//		propertyExtractor.extractProperties(LLStorage, 0) ;
//		ParsedCategory cat = LLStorage.getCategory() ;
//		XMLInterface.saveParsedCategory( rootPath + RawDataExtractor.RAW_DATA_FILE_NAME , cat, 0 ) ;
		

/*extract pictures*/
		MediaWikiUniverse universe = new MediaWikiUniverse( universePath, bigCategoryName ) ;
		PictureExtractor.getInstance().extractPictures(universe) ;


/*serialization*/		
//		MediaWikiUnivrse universe = new MediaWikiUnivrse( universePath, bigCategoryName ) ;
//		IRealm<Object> realm = universe.getRealm( null, null, null ) ;
//		UniverseObjectCollection collection = new UniverseObjectCollection( universe ) ;
//		ObjectCollectionWriter writer = new ObjectCollectionWriter() ;
//		writer.setFactory( new WriterFactory() ) ;
//		writer.append( collection ) ;
//		
//		File f = new File( universePath + "/serialized.dat" ) ;
//		FileOutputStream fos = new FileOutputStream(f) ;
//		DataOutputStream dos = new DataOutputStream(fos) ;
//		writer.serialize( dos ) ;
		
		return IApplication.EXIT_OK;
	}


	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#stop()
	 */
	public void stop() {
		// nothing to do
	}
}
